import { SHOP_API } from '../config/env.js'
export default function request(options) {
  // 如果没有http那么用SHOP_API
  let requestURL = options.url
  if (!/^http/.test(options.url)) {
    requestURL = /^\//.test(options.url)
      ? `${SHOP_API}${options.url}`
      : `${SHOP_API}/${options.url}`
  }
  return new Promise((resolve, reject) => {
    wx.request({
      //后台请求
      url: requestURL,
      method: options.method,
      data: options.data,
      headers: {
        'content-type': 'application/json'
      },
      success: function(response) {
        const res = response.data
        console.log(res)
        if (res.status !== 200) {
          reject(res || _responseFail)
        } else {
          resolve(res.data === undefined ? {} : res.data)
        }
      },
      fail: function(err) {
        reject(err)
      }
    })
  })
}

const _responseFail = {
  status: -1,
  info: '服务器繁忙，请稍后重试'
}
